from utils import df_into_db, read_sql
import os
import pandas as pd


def get_exist_data():
    # 查询每个合约最后时间以及返回最后一条数据  这种写法性能最优
    sql = f"""
    SELECT *
    FROM (
        SELECT *,
               ROW_NUMBER() OVER (PARTITION BY symbol ORDER BY datetime DESC) as rn
        FROM all_market_funding_rate
    ) ranked
    WHERE rn = 1;
    """
    df = read_sql(sql, db_name="dataloader")
    return df


def main():
    path = "E:/data/funding_rate/all_market_funding_rate"
    files = os.listdir(path)
    exist_data = get_exist_data()
    for file in files:
        # if file != "FLMUSDT_funding_rate.csv":
        #     continue
        # print(file)
        symbol = file.replace("_funding_rate.csv", "")  # 注意: 有些合约的名字含有下划线
        df = pd.read_csv(os.path.join(path, file))
        assert symbol == df.iloc[0]["symbol"], "文件和内容必须一致"
        symbol_df = exist_data[exist_data['symbol'] == symbol]
        try:
            max_datetime = symbol_df.iloc[0]["datetime"]
            df = df[df.datetime >= str(max_datetime)]
            assert df.iloc[0]["funding_rate"] == symbol_df.iloc[0]["funding_rate"]
        except Exception as e:
            print(f"read file:{file}异常:{e}")
            continue
        df = df[1:]
        if not df.empty:
            df["type"] = symbol_df.iloc[0]['type']
            df.drop(["time", "date"], axis=1, inplace=True)  # drop掉time和date列 datetime信息完备
            df_into_db(df, "dataloader", "all_market_funding_rate")


if __name__ == "__main__":
    main()